import { Effect } from '../../typings';
import { Reducer } from 'redux';
import { getMenuInfo } from '../services/loginServices';
import { hehLocalStorage } from '@/utils/utils';
import { history } from 'umi';

export interface ModelType {
  namespace: string;
  state: MainLayoutModelType;
  effects: {
    getMenuData: Effect;
  };
  reducers: {
    setCollapsed: Reducer;
    setOpenKeys: Reducer;
    setChildrenCollapsed: Reducer;
    setSelectedKeys: Reducer;
    setMenuData: Reducer;
    setTopTabs: Reducer;
    deleteTopTabs: Reducer;
    resetTopTabs: Reducer;
  };
}

export interface MainLayoutModelType {
  collapsed?: boolean;
  childrenCollapsed?: boolean;
  layoutType?: 'side' | 'top';
  theme?: 'light' | 'dark';
  openKeys: string[];
  selectedKeys: string[];
  childSider: boolean;
  menuData: any[];
  topTabs: any[];
}

const Model: ModelType = {
  namespace: 'MainLayoutModel',

  state: {
    collapsed: false,
    childrenCollapsed: false,
    layoutType: 'top',
    theme: 'light',
    openKeys: [''],
    selectedKeys: [''],
    childSider: true,
    menuData: [],
    topTabs: [],
  },

  effects: {
    *getMenuData({ payload }, { call, put }) {
      let response = [];
      if (!hehLocalStorage.hasItem('menuData')) {
        response = yield call(getMenuInfo, payload);
        hehLocalStorage.setItem('menuData', response);
      } else {
        response = hehLocalStorage.getItem('menuData');
      }
      if (history.location.pathname === '/') {
        if (response[0]?.children.length > 0) {
          history.push(response[0]?.children[0]?.path);
          yield put({
            type: 'setSelectedKeys',
            payload: response[0]?.children[0]?.path,
          });
          yield put({
            type: 'setTopTabs',
            payload: {
              path: response[0]?.children[0]?.path,
              title: response[0]?.children[0]?.name,
            },
          });
        } else {
          history.push(response[0]?.path);
          yield put({
            type: 'setSelectedKeys',
            payload: response[0]?.path,
          });
        }
      }

      yield put({
        type: 'setMenuData',
        payload: response,
      });
    },
  },

  reducers: {
    setTopTabs(state, { payload }) {
      let tag = true;
      state.topTabs.forEach((n: any) => {
        if (n.path === payload.path) {
          tag = false;
        }
      });
      hehLocalStorage.setItem(
        'topTabs',
        !tag ? state.topTabs : state.topTabs.concat([payload]),
      );
      return {
        ...state,
        topTabs: !tag ? state.topTabs : state.topTabs.concat([payload]),
      };
    },
    resetTopTabs(state, { payload }) {
      console.log(payload);
      return {
        ...state,
        topTabs: payload,
      };
    },
    deleteTopTabs(state, { payload }) {
      state.topTabs = state.topTabs.filter((n: any) => n.path !== payload);
      hehLocalStorage.setItem('topTabs', state.topTabs);
      return {
        ...state,
        topTabs: state.topTabs,
      };
    },

    setMenuData(state, { payload }) {
      return {
        ...state,
        menuData: payload,
      };
    },
    setCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
    setChildrenCollapsed(state, { payload }) {
      return {
        ...state,
        childrenCollapsed: payload,
      };
    },
    setOpenKeys(state, { payload }) {
      return {
        ...state,
        openKeys: payload,
      };
    },
    setSelectedKeys(state, { payload }) {
      return {
        ...state,
        selectedKeys: payload,
      };
    },
  },
};

export default Model;
